// ActionScript file
import mx.controls.VideoDisplay;
import mx.core.Application;
import mx.core.UIComponent;
import mx.graphics.codec.JPEGEncoder;



private function containsUrl(txt: String):Boolean {
	var pattern:RegExp = /([a-z0-9_-]+\.[a-z0-9_-]+)/gi;
	return (pattern.test(txt));
}

private function containsEmail(txt: String):Boolean {
	var pattern:RegExp = /[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}/gi
	return (pattern.test(txt));
}

private function filterTxt(txt:String):String {
    var pattern:RegExp = new RegExp( Application.application.badwords , "gi");
    txt = txt.replace(pattern,"***");
    return txt;
}

private function getBitmapData( target : UIComponent ) : BitmapData {
    var bd : BitmapData = new BitmapData( target.width, target.height );
    var m : Matrix = new Matrix();
    bd.draw( target, m );
    return bd;
   }

public function saveJpeg(im:Image,picture:String):void {
	var bd : BitmapData = getBitmapData(im);
	var jpgEncoder:JPEGEncoder = new JPEGEncoder(85);
	var jpgStream:ByteArray = jpgEncoder.encode(bd);
	//jpgStream.compress();
	
	Application.application.gateway.call("vcb.saveBitmap", new Responder(onResult,null),jpgStream,picture);
	function onResult ( result : Object ) : void {
			//saveJpegCompleted();
			return
		}	
}

public function takeSnapshot(im:Image,vd:VideoDisplay):void{
    var bd : BitmapData = getBitmapData(vd);
    im.source = new Bitmap( bd );
}

	
public function hiliteURLs(msg:String):String
	{	//escape all <
		//-
		var escaped:String = "";
		var ltPos:int = msg.indexOf("<");
		while (ltPos != -1) {
			escaped = msg.substring(0, ltPos) + "&lt;" + msg.substring(ltPos+1,msg.length);
			msg = escaped;
			ltPos = msg.indexOf("<");
		}
		//escape all >
		escaped = "";
		ltPos = msg.indexOf(">");
		while (ltPos != -1) {
			escaped = msg.substring(0, ltPos) + "&gt;" + msg.substring(ltPos+1,msg.length);
			msg = escaped;
			ltPos = msg.indexOf(">");
		}
		
		var url_begin:int = msg.indexOf("http:");
		if ( url_begin == -1 )
			url_begin = msg.indexOf("www.");
	
		if ( url_begin == -1 )
			return msg;
		
		var hilited:String = msg.substring(0, url_begin);
		var url_end:int = msg.indexOf( " ", url_begin );
		
		var urlstr:String = "";
		if ( url_end == -1 )
			urlstr = msg.substring(url_begin);
		else
			urlstr = msg.substring(url_begin, url_end);

		var urlref:String = urlstr;
		if ( urlstr.indexOf("www.") == 0 )
			urlref = "http://" + urlstr;

		var trailer:String = "";
		if ( url_end != -1 )
			trailer = hiliteURLs( msg.substring(url_end) );
		
		hilited += "<font color=\"#0000FF\"><u><a href=\"" + urlref + "\" target=\"_blank\">" + urlstr + "</a></u></font>" + trailer;

		return hilited;
	}  			
public function rgbToHex(color:uint):String{
			   var c:String = "00000" + color.toString(16)
			   var e:int = c.length;
			   c = c.substring(e - 6, e);
			   return c.toUpperCase();
}
private function capitalizeFirstLetter(word:String):String    {
	var firstLetter:String = word.substr(0, 1);
	firstLetter = firstLetter.toUpperCase();
	var otherLetters:String = word.substring(1);
	word = firstLetter + otherLetters;
	return word;
}